#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;
class Solution
{
public:
    int firstUniqChar(string s)
    {
        // char index count
        unordered_map<char, int> ans;
        int sLen = s.size();

        for (int i = 0; i < sLen; i++)
        {
            // ans[s[i]][i]++;
            if (ans.find(s[i]) == ans.end())
            {
                ans.insert({s[i], i});
            }
            else
            {
                ans.at(s[i]) -= sLen;
            }
        }
        int res = sLen;
        for (auto r : ans)
        {
            cout << r.first << ":" << r.second << endl;
            if (r.second >= 0 && res > r.second)
            {
                res = r.second;
            }
        }
        cout << (res == sLen ? -1 : res) << endl;
        return res == sLen ? -1 : res;
    }

    int firstUniqChar_2(string s)
    {
        int chars = 256;
        int n = s.size();

        int arr[chars];
        fill(arr, arr + chars, 0);

        for (int i = 0; i < n; i++)
        {
            arr[s[i]]++;
        }

        for (int i = 0; i < n; i++)
        {
            if (arr[s[i]] == 1)
            {
                return i;
            }
        }
        return -1;
    }

    int firstUniqCha_3r(string s)
    {
        vector<int> freq(26, 0);

        for (auto c : s)
        {
            freq[c - 'a'] += 1;
        }

        for (int i = 0; i < s.length(); i++)
        {
            if (freq[s[i] - 'a'] == 1)
            {
                return i;
            }
        }
        return -1;
    }
};
int main()
{
    Solution s;
    s.firstUniqChar("leetcode");
    return 0;
}
